<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/12
 * Time: 11:09
 */

namespace HiCommon\Repository;

use HiCommon\Model\ActivityPush;

class ActivityPushRepository extends BaseRepository
{

    /**
     * 分页获取全部数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all($arr_option = [])
    {
        return ActivityPush::commonget($arr_option);
    }

    /**
     * 分页获取全部数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_all_to_now($arr_option = [])
    {
        return ActivityPush::where('start_time','<=',time())->where('over_time','>',time())->commonget($arr_option);
    }



    /**
     * 分页获取生效的活动数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list_to_now( array $arr_option = [])
    {
        return ActivityPush::where('start_time','<=',time())->where('over_time','>',time())->commonpage($arr_option);
    }


    /**
     * 分页获取数据
     *
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list(array $arr_search = [] , array $arr_option = [])
    {
        return ActivityPush::where(
            function ($query)
            use($arr_search) {
                if( !empty($arr_search['title']) ){
                    $query->where('title','like','%'.$arr_search['title'].'%');
                }
                if( !empty($arr_search['type']) ){
                    $query->where('type',$arr_search['type']);
                }
                if( !empty($arr_search['jincheng']) ){
                    $query->where('status',1);

                    switch ( $arr_search['jincheng'] ){
                        case 1:    //未开始
                            $query->where('start_time','>',time());
                            break;
                        case 2:     //进行中
                            $query->where('start_time','<=',time());
                            $query->where('over_time','>=',time());
                            break;
                        case 3:     //已结束
                            $query->where('over_time','<',time());
                            break;
                    }
                }

            })->commonpage($arr_option);
    }

    /**
     * 通过where分页获取数据
     *
     * @param array $arr_where
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_list_by_where($arr_where,$arr_option = [])
    {
        return ActivityPush::where($arr_where)->commonpage($arr_option);

    }

    /**
     * 根据条件统计数量
     *
     * @param array $arr_where
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_by_where($arr_where)
    {
        return ActivityPush::where($arr_where)->count();
    }

    /**
     * 根据条件统计数量
     *
     * @param array $arr_where
     *
     * @return mixed   betwnn
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_count_by_where_to_time( int $time , array $arr_where , int $id = null )
    {
        if( $id ){  //编辑时将当前数据排除
            return ActivityPush::where('start_time','<=',$time)->where('over_time','>', $time)->where( $arr_where )->whereNotIn('id',[$id])->count();
        }else{
            return ActivityPush::where('start_time','<=',$time)->where('over_time','>', $time)->where( $arr_where )->count();
        }
    }

    /**
     * 根据条件统计总和
     *
     * @param array  $arr_where
     * @param string $s_field
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_sum_by_where($arr_where = [],$s_field = 'mark')
    {
        return ActivityPush::where($arr_where)->sum($s_field);
    }

    /**
     * 添加一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function add_one( array $arr_input)
    {
        return ActivityPush::create($arr_input);
    }

    /**
     * 查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_one(int $n_id, $arr_option = [])
    {
        return ActivityPush::commonfind($n_id, $arr_option);
    }

    /**
     * 根据条件查询一条数据
     *
     * @param int $n_id
     * @param array $arr_option
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function get_to_where_one( $where = [] , $arr_option = [] )
    {
        return ActivityPush::where( $where )->commonFirst( $arr_option );
    }




    /**
     * 修改一条数据
     *
     * @param array $arr_input
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function edit_one(array $arr_input)
    {
        return ActivityPush::where('id',$arr_input['id'])->update($arr_input);
    }

    /**
     * 删除一条数据
     *
     * @param int $n_id
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete_one(int $n_id)
    {
        return ActivityPush::where('id', $n_id)->commondelete();
    }

    /**
     * 修改一条数据状态
     *
     * @param int $n_id
     * @param int $n_state
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function change_one_state(int $n_id,int $n_state)
    {
        return ActivityPush::where('id',$n_id)->update(['status'=>$n_state]);

    }

    /**
     * 字段自增
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function increase_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return ActivityPush::where('id',$n_id)->increment($s_field,$n_step);
    }

    /**
     * 字段自减
     *
     * @param int    $n_id
     * @param string $s_field
     * @param int    $n_step
     *
     * @return mixed
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function decrease_num($n_id,$s_field = 'commend_num',$n_step = 1)
    {
        return ActivityPush::where('id',$n_id)->decrement($s_field,$n_step);
    }


}